package com.satyam.smooth.dao.testUtil;

import com.satyam.smooth.util.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;

/**
 *
 * @author srt
 */
public class DBUnitTestHelper {

    private static final String testDir = "dbUnit";
    private static final String driverClassStr = "com.mysql.jdbc.Driver";
    private static final String jdbcConnectionStr = "jdbc:mysql://localhost/smoothinventory";
    private static final String userName = "root";
    private static final String password = "";
    private static final Logger log = LoggerFactory.make();

    public static IDatabaseConnection getConnection() throws ClassNotFoundException,
            DatabaseUnitException,
            SQLException {
        Class driverClass = Class.forName(driverClassStr);
        Connection jdbcConnection = DriverManager.getConnection(jdbcConnectionStr, userName, password);
        return new DatabaseConnection(jdbcConnection);
    }

    public static void importDatabase(String dbUnitXmlFileName) throws ClassNotFoundException,
            DatabaseUnitException,
            IOException,
            SQLException {
        File file = new File(testDir, dbUnitXmlFileName);
        IDatabaseConnection connection = getConnection();
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(file);
        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
    }
    

}
